import { FormInstance } from "ant-design-vue";
import { ref, watchEffect } from "vue";

/**
 * @description: 重置表单
 * @param {Function} fn
 * @return {*}
 * @example: const { resetForm } = useResetForm(() => {
 *  // to do
 * });
 * @returns
 */
export const useDefaultForm = (submitFn: () => any) => {
  const formRef = ref<FormInstance>();
  const resetForm = (fn?: () => void) => {
    formRef.value?.resetFields();
    fn && fn();
  };
  // 清除验证结果
  const clearValidate = (names: any[]) => {
    formRef.value?.clearValidate(names);
  };

  // 校验函数，能够回调成功和失败的函数
  const validate = async (successCb?: () => void, errFn?: () => void) => {
    try {
      await formRef.value?.validate();
      successCb && successCb();
    } catch (error) {
      errFn && errFn();
    }
  };

  // 校验并提交
  const submit = async () => submitFn && validate(submitFn);

  return {
    formRef,
    resetForm,
    clearValidate,
    validate,
    submit,
  };
};

const useFn = <T extends any[], P>(fn: (...args: T) => P) => {
  return fn;
};

const fn2 = useFn((a, b) => a + b);
fn2(123, 123123);
